package com.andengine.editor.data;

import com.andengine.editor.FileUtils;
import com.andengine.editor.Properties;
import com.swingson.exceptions.ErrorMessageException;
import java.io.File;

public class TextureFile extends File {

    public TextureFile(String pathname) {
        super(getPath(pathname));
        String path = getPath(pathname);
        if (!isFileInAssetFolder(path)) {
            throw new ErrorMessageException("File " + path + " is not in asset folder");
        }
    }

    private static String getPath(String filePath) {
        if (isFileInAssetFolder(filePath)) {
            return filePath;
        }
        return Properties.get().getProperty(Properties.TEXTURE_PATH) + filePath;
    }

    public String getSmallPath() {
        return getAbsolutePath().substring(Properties.get().getProperty(Properties.TEXTURE_PATH).length());
    }

    @Override
    public String toString() {
        return getSmallPath();
    }

    public static boolean isFileTexture(String fileName) {
        String extension = com.gooddies.utils.FileUtils.getFileExtension(fileName);
        if (extension == null) {
            return false;
        }
        extension = extension.toLowerCase();
        if (extension.equals("jpg") || extension.equals("png") || extension.equals("jpeg") || extension.equals("bmp")) {
            return true;
        }
        return false;
    }

    public static boolean isFileInAssetFolder(String path) {
        return FileUtils.checkFileInDirectory(Properties.get().getProperty(Properties.TEXTURE_PATH), path);
    }
}
